﻿using System;
using System.Collections.Generic;
using System.Text;


namespace WindowsFormsForControlTest
{
    public interface IDataManager
    {
        //bool AppInfoAdd(AppInfo info);
        //void AppDelete(AppInfo info);
        //void AppRepale(AppInfo oldInfo, AppInfo newInfo);
        AppInfo AppSearch(string name);
    }

    class DataManager : IDataManager
    {
        private List<AppInfo> allAppInfo;
        public Dictionary<string, List<AppInfo>> AppInfoGroups = new Dictionary<string, List<AppInfo>>();

        /// <summary>
        /// 单例
        /// </summary>
        private static DataManager instance;
        public static DataManager _Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataManager();
                    return instance;
                }
                return instance;
            }
        }

        public DataManager()
        {
            allAppInfo = XmlHelper._Instance.AllAppPath;
            MakeGroup();         
        }

        /// <summary>
        /// 分组
        /// </summary>
        private void MakeGroup()
        {
            if (AppInfoGroups == null) return;

            for (int i = 0; i < allAppInfo.Count; i++)
            {
                string groupName = allAppInfo[i].GroupName;
                if (!AppInfoGroups.ContainsKey(groupName))
                {
                    List<AppInfo> newAppGroup = new List<AppInfo>();
                    newAppGroup.Add(allAppInfo[i]);
                    AppInfoGroups.Add(allAppInfo[i].GroupName, newAppGroup);
                }
                else
                {
                    AppInfoGroups[groupName].Add(allAppInfo[i]);
                }
            }
        }

        public AppInfo AppSearch(string customAppName)
        {
            if (customAppName == "") return null;

            foreach (AppInfo info in allAppInfo)
            {
                if (info.CustomAppName == customAppName)
                    return info;
            }

            return null;
        }

    }
}
